1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <bits/stdc++.h> #define rep(i, x, y) for (int i = x; i <= y; i++) #define lowbit(x) ((x) & (-(x))) #define int long long using namespace std;
const int N = 1e5 + 10; typedef long long ll; int n, a[N], ans[N], C[N], num[N];
void add(int x, int v) { for (; x <= n + 1; x += lowbit(x)) C[x] += v; }
int query(int x) { if (!x) return 0; int ret = 0; for (; x; x -= lowbit(x)) ret += C[x]; return ret; }
signed main() { cin >> n; rep(i, 1, n) { scanf("%lld", &a[i]); ++a[i]; } int tot = 0; rep(i, 1, n) { num[i] = i - 1 - query(a[i]); tot += num[i]; add(a[i], 1); } memset(C, 0, sizeof(C)); rep(i, 1, n) add(1, -num[i]), add(a[i] + 1, num[i]); int lst = 0; rep(i, 1, n) { printf("%lld\n", query(i) + tot); } return 0; }
|